﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using System.Data.Linq.SqlClient;

namespace DAL
{
    public class DALSanPham
    {

        public static List<BLL.BLLSanPham> GetAll()
        {

            var sps = (from sp in DALCommon.TB.SanPhams
                       select
                 new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham,DALLoaiSanPham.GetById(sp.IDLoaiSanPham)[0].Ten.ToString(), sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));

            return sps.ToList<BLLSanPham>();
        }
        //lay 6 sp moi' trong trang default
        public static List<BLL.BLLSanPham> Get6()
        {

            var sps = (from sp in DALCommon.TB.SanPhams
                      select
                new BLLSanPham(sp.IDSanPham,sp.IDLoaiSanPham, sp.TenSanPham,sp.KieuDang,sp.ChatLieu,sp.KickCo,sp.Mau,sp.XuatXu,sp.DonGia.ToString(),sp.Hinh)).Take(6);

            return sps.ToList<BLLSanPham>();           
        }
        //sp theo danh muc
        public static List<BLL.BLLSanPham> GetSPDM(BLLSanPham bllsp)
        {
            var sps = (from sp in DALCommon.TB.SanPhams
                       where sp.IDLoaiSanPham.ToString()==bllsp.Idloai
                       select
                 new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham, sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));

            return sps.ToList<BLLSanPham>();
            
        }

        public static List<BLL.BLLSanPham> GetSPTK(BLLSanPham sanpham)
        {
            ThanhBaoDataContext data = DALCommon.TB;
            var result = from sp in data.SanPhams
                         select sp;

            if (sanpham.Tensp != "")
            {
                result = result.Where(sp =>SqlMethods.Like(sp.TenSanPham,"%"+sanpham.Tensp+"%"));
            }
            if (sanpham.Idloai !="0")
            {
                result = result.Where(sp=>sp.IDLoaiSanPham==sanpham.Idloai);
            }
            if (sanpham.Dongia == "1")
            {
                result = result.Where(sp=>Convert.ToDecimal(sp.DonGia)<=50000);
            }
            if (sanpham.Dongia == "2")
            {
                result = result.Where(sp => Convert.ToDecimal(sp.DonGia) > 50000 && Convert.ToInt32(sp.DonGia) <=100000);
            }
            if (sanpham.Dongia == "3")
            {
                result = result.Where(sp => Convert.ToDecimal(sp.DonGia) > 100000 && Convert.ToInt32(sp.DonGia) <= 200000);
            }
            if (sanpham.Dongia == "4")
            {
                result = result.Where(sp =>Convert.ToDecimal(sp.DonGia) > 200000);
            }
            List<BLLSanPham> list = new List<BLLSanPham>();
            foreach (SanPham sp in result)
            {
                list.Add(new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham, sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));
            }

                return list;
        }

        // hien thi chi tiet san pham
        public static List<BLL.BLLSanPham> ShowChiTiet(BLLSanPham sanpham)
        {
            var sps = (from sp in DALCommon.TB.SanPhams
                       where sp.IDSanPham ==sanpham.Idsp
                       select
                 new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham, sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));
            return sps.ToList<BLLSanPham>();
        }

        //them san pham moi
        public static string AddSanPham(BLLSanPham sanpham)
        {
            ThanhBaoDataContext tmdt = new ThanhBaoDataContext();
            SanPham sp = new SanPham();
            sp.IDSanPham = sanpham.Idsp;
            sp.IDLoaiSanPham = sanpham.Idloai;
            sp.TenSanPham = sanpham.Tensp;
            sp.KieuDang = sanpham.Kieudang;
            sp.ChatLieu = sanpham.Chatlieu;
            sp.KickCo = sanpham.Kichco;
            sp.Mau = sanpham.Mau;
            sp.XuatXu = sanpham.Xuatxu;
            sp.DonGia = sanpham.Dongia;
            sp.Hinh = sanpham.Hinh;
            DALCommon.TB.SanPhams.InsertOnSubmit(sp);
            try
            {
                DALCommon.TB.SubmitChanges();
            }
            catch (Exception ex)
            { 
                
            }
            return sp.IDSanPham;
            
        }
        //sua san pham
        public static string Edit(BLLSanPham sanpham)
        {
            SanPham sp = DALCommon.TB.SanPhams.Single<SanPham>(d => d.IDSanPham ==sanpham.Idsp);
            sp.IDLoaiSanPham=sanpham.Idloai;
            sp.KickCo=sanpham.Kichco;
            sp.KieuDang=sanpham.Kieudang;
            sp.Mau=sanpham.Mau;
            sp.XuatXu = sanpham.Xuatxu;
            sp.TenSanPham=sanpham.Tensp;
            sp.ChatLieu=sanpham.Chatlieu;
            sp.DonGia=sanpham.Dongia;
            sp.Hinh = sanpham.Hinh;
            DALCommon.TB.SubmitChanges();
            return sp.IDSanPham;
        }
        //xóa sản phẩm
        public static void Delete(BLLSanPham sanpham)
        {
            SanPham sp = DALCommon.TB.SanPhams.Single<SanPham>(n => n.IDSanPham == sanpham.Idsp);
            DALCommon.TB.SanPhams.DeleteOnSubmit(sp);
            DALCommon.TB.SubmitChanges();
        }
        //lay san pham theo id
        public static List<BLL.BLLSanPham> GetbyId(BLLSanPham sanpham)
        {
            var sps = (from sp in DALCommon.TB.SanPhams
                       where sp.IDSanPham == sanpham.Idsp
                       select
                 new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham, sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));

            return sps.ToList<BLLSanPham>();
        }
    }
}
